package com.terrier.utils
{

    /**
     * 数组迭代器.
     * @author Jarvis.weng
     */
    public class WxArrayIterator
    {
        private var array:Array;

        private var forward:Boolean;

        private var index:int;

        private var end:int;

        private var step:int;

        /**
         * 构造函数
         * @param array 用来迭代的数组
         * @param forward 正向迭代(从头至尾)还是反向迭代(从尾至头)
         */
        public function WxArrayIterator(array:Array, forward:Boolean)
        {
            this.array = array;
            this.forward = forward;

            reset();
        }

        /**
         * 重置迭代器
         */
        public function reset():void
        {
            if (array == null)
            {
                index = 0;
                end = 0;
                step = 0;
                return;
            }

            if (forward)
            {
                index = 0;
                end = array.length;
                step = 1;
            }
            else
            {
                index = array.length - 1;
                end = -1;
                step = -1;
            }
        }

        /**
         * 返回下一个数组元素
         * @return
         */
        public function next():*
        {
            if (index == end)
            {
                return null;
            }
            var element:* = array[index];
            index += step;
            return element;
        }
    }
}
